/**
 * 角色权限配置
 */
var themeConfig = (function(){
	//角色面板
	var rolePanelInit=function(){
		$('#roleTree').panel({  
			  width:260,  
			  height:800,
			  title: '角色',
			  tools: [ {  
			    iconCls:'icon-del',  
			    handler:function(){alert('save')}  
			  }]
		});
		 $('#roleTree').closest(".panel").css({"display":"inline-block","margin-left":"10px","float":"left"});
	};
	
	//数据专题面板
	var themePanelInit=function(){
		$('#themeTree').panel({  
			  width:260,  
			  height:800,
			  title: '数据专题',
			  tools:[
		  				{  
						    iconCls:'icon-save',  
						    handler:saveRoleResource
						  }
		  				
		  				
		  			 ]
		});
		 $('#dataTheme').closest(".panel").css({"padding":"0px 10px","margin-left":"10px","float":"left","margin-right":"20px"});
	};
	
	//功能模块面板
	var modulePanelInit=function(){
		$('#moduleTree').panel({  
			  width:260,  
			  height:800,
			  title:'功能模块',
			  tools: [{
				    iconCls:'icon-add',  
				    handler:addModule
				  },{  
					    iconCls:'icon-edit',  
					    handler:editModule  
					  },{  
				    iconCls:'icon-del',  
				    handler:deleteModule
				  },{  
					    iconCls:'icon-save',  
					    handler:saveRoleModule
					  }]
		});
		 $('#moduleTree').closest(".panel").css({"display":"inline-block" ,"margin-left":"20px","float":"left","margin-right":"20px"});
	};
	
	//角色数据
	var roleTreeInit=function(){
		//debugger
		$("#roleTreeData").tree({
		    url:basePath+'/roleController/tree',
			lines:true,//默认是false
  			checkbox:false,//默认是false   
  			onlyLeafCheck:false,//默认是false
  			cascadeCheck:true,//默认是true
  			parentField:'pid',
  			dnd:false,//默认是false, 是否能够拖拽 ； 设置为true的时候,该节点就是可以拖拽的，可以移动位置
  			checkbox:false,
  			onLoadSuccess:function(node, data){
  				//debugger
  			},onSelect:reloadTrees
		});
	};
	
	//数据专题树数据
	var themeTreeInit=function(){
		 
		$("#themeTreeData").tree({
		    url:basePath+'/DataThemeController/tree',
			lines:true,//默认是false
  			checkbox:false,//默认是false   
  			onlyLeafCheck:false,//默认是false
  			cascadeCheck:true,//默认是true
  			parentField:'pid',
  			dnd:false,//默认是false, 是否能够拖拽 ； 设置为true的时候,该节点就是可以拖拽的，可以移动位置
  			checkbox:true,
  			onLoadSuccess:function(node, data){
  				//debugger
  			}
		});
	};
	
	//模块树数据
	var moduleTreeInit=function(){
		$("#moduleTreeData").tree({
		    url:basePath+'/moduleController/tree',
			lines:true,//默认是false
  			checkbox:false,//默认是false   
  			onlyLeafCheck:false,//默认是false
  			cascadeCheck:true,//默认是true
  			parentField:'pid',
  			dnd:false,//默认是false, 是否能够拖拽 ； 设置为true的时候,该节点就是可以拖拽的，可以移动位置
  			checkbox:true,
  			onLoadSuccess:function(node, data){
  				//debugger
  			}
		});
	};
	
	//新增模块
	var addModule = function(){
	    	//先获取当前选中的节点
	    	var selected = ''//$('#layout_center_dataThemeTree').tree('getSelected');
	    	$('#addDataTheme')[0].reset(); //$('#moduleTreeAddPopWin').window('close'); 关闭后需重置,不然界面数据依旧保留
	    	if(selected != null){
	    		$('#moduleTreeAddPopWin').window({
		    	    width:500,
		    	    height:220,
		    	    modal:true,
		    	    title:'添加数据',
		    	    minimizable:false,
		    	    maximizable:false,
		    	    closable:true,
		    	    collapsible:false,
		    	    constrain:true,
		    	    left:450,
		    	    top:200 
		    	});
	    		$("form#addDataTheme #okBtn").unbind('click').click(function(){
	    		   var parentNodeId = $('#moduleTreeData').tree('getSelected');
	    		   if(parentNodeId){
	    			   parentNodeId = parentNodeId.id
	    		   }else{
	    			   parentNodeId ='0'
	    		   }
	    		   $('form#addDataTheme input[name=parentId]').val(parentNodeId);
		    		
		    		
		    		$('#addDataTheme').form({
			            url : '/sddc/client/backgroundManagement/moduleController/add',
			            onSubmit : function() {
			            	//var isValid = $(this).form('validate');
			            },
			            success : function(result) {
			            	result = $.parseJSON(result);
			            	//debugger
			            	if(result.success){
			            		$('#moduleTreeAddPopWin').window('close');
			            		$('#moduleTreeData').tree('reload')
			            		
			            	}else{
			            		alert('操作失败')
			            	}
			            }
		    		})
		    		
		    		$('form#addDataTheme').submit();
		    		
		    	});
	    	}else{
	    		$.messager.show({
		        	title:'提示',
		        	msg:"请选择一个功能模块"
		        });
	    	}
	};
	
	//编辑模块
	var editModule = function(){
    	//先获取当前选中的节点
    	var selected =  $('#moduleTreeData').tree('getSelected');
    	$('#addDataTheme')[0].reset(); //$('#moduleTreeAddPopWin').window('close'); 关闭后需重置,不然界面数据依旧保留
    	if(selected != null){
    		$('#moduleTreeAddPopWin').window({
	    	    width:500,
	    	    height:220,
	    	    modal:true,
	    	    title:'编辑',
	    	    minimizable:false,
	    	    maximizable:false,
	    	    closable:true,
	    	    collapsible:false,
	    	    constrain:true,
	    	    left:450,
	    	    top:200 
	    	});
    		
    		//赋值
    		var selectNodeId = $('#moduleTreeData').tree('getSelected').id;
    		$.ajax({
        		type:"GET",
        		url:"/sddc/client/backgroundManagement/moduleController/getEntityById?id="+selectNodeId,
        		success:function(resp){
        			 result = $.parseJSON(resp);
        			 if(result){
        				 
        				 $('form#addDataTheme input[name=name]').val(result.modelName);
        				 if(result.modelIndex)
        				 $('#moduleIndex').numberbox('setValue',result.modelIndex);
        				 $('#parentModuleId').val(result.parentId);
        				 if(result.description)
        				 $('#moduleDescription').val(result.description);
        				 $('#moduleId').val(result.id);
        			 }else{
        				 alert("查询失败")
        			 }
        		}
        	});
    		
    		//编辑保存
    		$("form#addDataTheme #okBtn").unbind('click').click(function(){
	    		
	    		$('#addDataTheme').form({
		            url : '/sddc/client/backgroundManagement/moduleController/edit',
		            onSubmit : function() {
		            	//var isValid = $(this).form('validate');
		            },
		            success : function(result) {
		            	result = $.parseJSON(result);
		            	 
		            	if(result.success){
		            		$('#moduleTreeAddPopWin').window('close');
		            		$('#moduleTreeData').tree('reload')
		            		
		            	}else{
		            		alert('操作失败')
		            	}
		            }
	    		})
	    		
	    		$('form#addDataTheme').submit();
	    		
	    	});
    	}else{
//    		$.messager.show({
//	        	title:'提示',
//	        	msg:"请选择要编辑的数据"
//	        });
    		alert('请选择要编辑的数据')
    	}
	};
	
	//删除模块
	var deleteModule = function(){
		var selected =  $('#moduleTreeData').tree('getSelected');
		if(selected!=null){
			var selectNodeId = $('#moduleTreeData').tree('getSelected').id;
			var g = $('#moduleTreeData').tree('getSelected');
			var length = $('#moduleTreeData').tree('getChildren',g.target).length;
			if(length>0){
				alert('需先删除子节点!!!')
				return false;
				
			}
			//debugger;
//			var flag = confirm('是否删除?');
			
		    $.messager.confirm("确认", '是否删除?', function (r) {  
		        if (r) {  
					$.ajax({
			    		type:"GET",
			    		url:"/sddc/client/backgroundManagement/moduleController/delete?id="+selectNodeId,
			    		success:function(resp){
			    			 result = $.parseJSON(resp);
			    			 if(result.success){
			    				 $('#moduleTreeData').tree('reload');
			    			 }else{
			    				 alert("操作失败")
			    			 }
			    		}
			    	});
		        }  
		    }); 
		    
	
		}else{
			alert('请选择要删除的数据')
		}
		
	};
	
	//重新读取模块树,专题树
	var reloadTrees = function(node, data){
		 
		//$("#themeTreeData").tree('reload' );
		//$("#moduleTreeData").tree('reload' );
		
		$.ajax({
    		type:"GET",
    		url:"/sddc/client/backgroundManagement/roleController/getPrivilege?id="+node.id,
    		success:function(resp){
    			resp = $.parseJSON(resp);
    			var arr = resp.split('|');
    			var moduleIds = arr[0].split(',')
    			var resourceIds = arr[1].split(',');
    			
    			//重置所有checked
    			
    			var t = $('#moduleTreeData').tree('getChildren');
    			for(var g=0;g<t.length;g++){
    				$('#moduleTreeData').tree('uncheck',t[g].target)
    				
    				for(var j=0;j<moduleIds.length;j++){
    					var moduleId = moduleIds[j];
    					console.log(t[g].id+'=='+moduleId);
    	    			   if(t[g].id==moduleId){
    	      			     
    	      			   if($('#moduleTreeData').tree('isLeaf',t[g].target) ){
    	      				 $('#moduleTreeData').tree('check',t[g].target)   //check
    	      			    }
    	      			     
    	      			   }
    				}
    			  
    			}
    			
    			   debugger;
    			  t = $('#themeTreeData').tree('getChildren');
    			for(var g=0;g<t.length;g++){
    				$('#themeTreeData').tree('uncheck',t[g].target)
    				for(var j=0;j<resourceIds.length;j++){
    					var resourceId = resourceIds[j];
    					console.log(t[g].id+'=='+resourceId);
    	    			   if(t[g].id==resourceId){
    	      			     
    	      			      
    	      			   if($('#moduleTreeData').tree('isLeaf',t[g].target) ){
    	      				 $('#themeTreeData').tree('check',t[g].target)   //check
    	      			   }
    	      			    
    	      			   }
    				}
    			  
    			}
    			
    			
    		}
    	});
		
		 
	};
	
	//保存角色-模块
	var saveRoleModule = function(){
		var roleSelected =  $('#roleTreeData').tree('getSelected');
		
		var selected = $('#moduleTreeData').tree('getChecked')
		var moeuleIds = '';
		for(var g = 0;g<selected.length;g++){
		  //是否为子叶
			if($('#moduleTreeData').tree('isLeaf',selected[g].target) ){
				moeuleIds+=(selected[g].id+',');
			}
		}
		
		var param = "roleId="+roleSelected.id+"&moduleIds="+moeuleIds
		
		$.ajax({
    		type:"POST",
    		url:"/sddc/client/backgroundManagement/roleController/grantModule?"+param,
    		success:function(resp){
    			 result = $.parseJSON(resp);
    			 if(result.success){
    				 alert("操作成功")
    			 }else{
    				 alert("操作失败")
    			 }
    		}
    	});
		
	};
	
	//保存角色-资源树
	var saveRoleResource = function(){
		var roleSelected =  $('#roleTreeData').tree('getSelected');
		
		var selected = $('#themeTreeData').tree('getChecked')
		if(roleSelected.length==0){
			alert('请选择角色')
			return false;
		}
		
		var resourceIds = '';
		
		for(var g = 0;g<selected.length;g++){
		  //是否为子叶
			if($('#themeTreeData').tree('isLeaf',selected[g].target) ){
				resourceIds+=(selected[g].id+',');
			}
		}
 
		
		var param = "roleId="+roleSelected.id+"&resourceIds="+resourceIds
		
		$.ajax({
    		type:"POST",
    		url:"/sddc/client/backgroundManagement/roleController/grantResource?"+param,
    		success:function(resp){
    			 result = $.parseJSON(resp);
    			 if(result.success){
    				 alert("操作成功")
    			 }else{
    				 alert("操作失败")
    			 }
    		}
    	});
		
	};
	
	
	var confirm  = function (msg, control) {  
	    $.messager.confirm("确认", msg, function (r) {  
	        if (r) {  
	            return true;  
	        }  
	    });  
	    return false;  
	}  
	
	return {
		init:function(){
			rolePanelInit();
			modulePanelInit();
			themePanelInit();
			
			roleTreeInit();
			themeTreeInit();
			moduleTreeInit();
		}
	}
})();